<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Permission;
use Illuminate\Http\Request;
use App\User;
use App\Http\Resources\UserResource;
use App\Http\Requests\Admin\UserRequest;

class UserController extends Controller
{
    /**
     * 用户列表
     * @param Request $request
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function index(Request $request)
    {
        $user = User::latest()->with('roles')->paginate($request->get('per_page', 10));
        return UserResource::collection($user);
    }

    /**
     * 新增一个用户
     * @param UserRequest $request
     * @return UserResource
     */
    public function store(UserRequest $request)
    {
        $user = User::create($request->all());
        return new UserResource($user);
    }

    /**
     * 用户详情
     * @param Request $request
     * @return UserResource
     */
    public function show(Request $request)
    {
        $user = auth()->user();
        activity('login')->causedBy($user)->log('登录成功，IP:' . request()->getClientIp());
        return new UserResource($user);
    }

    /**
     * 删除一个用户
     * @param Request $request
     * @param User $user
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy(Request $request, User $user)
    {
        $user->delete();
        return response()->json(['status' => 200, 'message' => "删除成功"]);
    }

    /**
     * 退出登录
     * @param Request $request
     * @param User $user
     * @return \Illuminate\Http\JsonResponse
     */
    public function logout(Request $request)
    {
        $user = auth('api')->user();
        auth('admin')->user()->token()->revoke();
        activity('login')->causedBy($user)->log('退出登录,IP:' . $request->getClientIp());
        return response()->json();
    }

    /**
     * 获取当前用户所拥有的菜单列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getMenuList(Request $request)
    {
        $menuList = [];
        $user = \Auth::guard('admin')->user();
        if ($user['email'] === "280498307@qq.com") {
            $menuList = Permission::where('is_menu', 1)->orderBy('sort', 'desc')->get()->toArray();
        } else {
            $user->loadMissing(['roles.permissions' => function ($query) {
                $query->where('is_menu', 1)->orderBy('sort', 'desc');
            }]);
            foreach ($user->roles as $role) {
                $menuList = array_merge($menuList, $role->permissions->toArray());
            }
        }
        $result = data_to_tree($menuList);
        return response()->json($result);
    }

    /**
     * 获取当前用户所拥有的权限列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getPermissionList(Request $request)
    {
        $menuList = [];
        $user = \Auth::guard('admin')->user();
        if ($user['email'] === "280498307@qq.com") {
            $menuList = Permission::all()->toArray();
        } else {
            $user->loadMissing(['roles', 'permissions']);
            foreach ($user->roles as $role) {
                $menuList = array_merge($menuList, $role->permissions->toArray());
            }
        }
        $result = data_to_tree($menuList);
        return response()->json($result);
    }

    /**
     * 为用户分配角色
     * @param Request $request
     * @param User $user
     * @return \Illuminate\Http\JsonResponse
     */
    public function changeRoleFromUser(Request $request, User $user)
    {
        $this->validate($request, [
            'role_id' => 'array'
        ],
            [
                'role_id.array' => '为数组',
            ]
        );
        $user->roles()->sync($request->get('role_id', []));
        return response()->json(['status' => 200, 'message' => 'success']);
    }
}
